<?php

namespace app\api\controller;


use app\common\model\product\UserAddress;

use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use think\Request;
use think\Response;


/**
 * 商品api
 * Class Product
 * @package app\api\controller
 */
class Product extends Wap
{
    /**
     * 收货地址列表
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function addressList(Request $request)
    {
        if (request()->isGet()) {
            $user = $this->checkToken();
            //$user['id'] = 13695;
            $data = [];
            $addressList = UserAddress::where('user_id', $user['id'])->select();
            if (count($addressList) > 0) {
                foreach ($addressList as $address) {
                    $data[] = [
                        'id' => $address->id,
                        'realname' => $address->realname,
                        'mobile' => $address->mobile,
                        'is_default' => $address->is_default,
                        'address_name' => $address->address_name . ' ' . $address->address_details,
                    ];
                }
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * 收货地址详情
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function addressInfo(Request $request)
    {
        if (request()->isGet()) {
            $get = $request->param();
            $id = $get['id'];
            if (!isset($id)) {
                $arr['code'] = 101;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();

            }
            $user = $this->checkToken();
            //$user['id'] = 13695;
            $addressInfo = UserAddress::where('user_id', $user['id'])->where('id', $id)->field('id,realname,mobile,remarks,is_default,province_id,city_id,area_id,address_details')->find();
            if (!$addressInfo) {
                $arr['code'] = 101;
                $arr['message'] = '地址不存在';
                Response::create($arr, 'json')->send();
                exit();

            }

            $addressInfo = $addressInfo->toArray();
            $addressInfo['province'] = Db::name('city')->where('citycode', $addressInfo['province_id'])->value('name');
            $addressInfo['city'] = Db::name('city')->where('citycode', $addressInfo['city_id'])->value('name');
            $addressInfo['area'] = Db::name('city')->where('citycode', $addressInfo['area_id'])->value('name');
            $arr['code'] = 200;
            $arr['data'] = $addressInfo;
            $arr['message'] = '参数错误';
            Response::create($arr, 'json')->send();
            exit();


        }
    }

    /**
     * 设置默认地址
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function addressDefault(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $id = $post['id'];
            if (!isset($id)) {
                $arr['code'] = 101;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            $user = $this->checkToken();
            //$user['id'] = 13695;
            $addressModel = UserAddress::where('user_id', $user['id'])->where('id', $id)->find();
            if (!$addressModel) {
                $arr['code'] = 101;
                $arr['message'] = '地址不存在';
                Response::create($arr, 'json')->send();
                exit();

            }
            UserAddress::where('user_id', $user['id'])->update([
                'is_default' => 0
            ]);
            $addressModel->is_default = 1;
            $addressModel->save();
            $arr['code'] = 200;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * 删除收货地址
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function addressDel(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $id = $post['id'];
            if (!isset($id)) {
                $arr['code'] = 101;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            $user = $this->checkToken();
            //$user['id'] = 13695;
            $addressModel = UserAddress::where('user_id', $user['id'])->where('id', $id)->find();
            if (!$addressModel) {
                $arr['code'] = 101;
                $arr['message'] = '地址不存在';
                Response::create($arr, 'json')->send();
                exit();

            }
            $addressModel->delete();

            $arr['code'] = 200;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * 添加修改收货地址
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function updateAddress(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            //$user['id'] = 13695;

            $id = getter($post, "id", 0);
            $remarks = getter($post, "remarks", '');

            $provinceId = $post['province_id'];
            $cityId = $post['city_id'];
            $areaId = $post['area_id'];
            $addressDetails = $post['address_details'];
            $isDefault = $post['is_default'];
            $isDefault = ($isDefault != 1) ? 0 : 1;
            $realname = $post['realname'];
            $mobile = $post['mobile'];
            $remarks = $post['remarks'];
            if (!is_numeric($provinceId) || !is_numeric($cityId) || !is_numeric($areaId)) {
                $arr['code'] = 101;
                $arr['message'] = '请填选地区';
                Response::create($arr, 'json')->send();
                exit();

            }
            if (!isset($mobile)) {
                $arr['code'] = 101;
                $arr['message'] = '请填写手机号';
                Response::create($arr, 'json')->send();
                exit();

            }
            if (!isset($realname)) {
                $arr['code'] = 101;
                $arr['message'] = '请填写收入姓名';
                Response::create($arr, 'json')->send();
                exit();

            }
            if (!isset($addressDetails)) {
                $arr['code'] = 101;
                $arr['message'] = '地址详情不能为空';
                Response::create($arr, 'json')->send();
                exit();

            }
            if ($id > 0) { //修改
                $model = UserAddress::where('user_id', $user['id'])->where('id', $id)->find();
                if (!$model) {
                    $arr['code'] = 101;
                    $arr['message'] = '地址不存在';
                    Response::create($arr, 'json')->send();
                    exit();

                }
                $model->update_time = time();
            } else { //添加
                $model = new UserAddress();
                $model->create_time = time();
            }
            $model->user_id = $user['id'];
            $model->province_id = $provinceId;
            $model->city_id = $cityId;
            $model->area_id = $areaId;
            $provinceName = Db::table('evt_city')->where('citycode', $provinceId)->value('name');
            $cityName = Db::table('evt_city')->where('citycode', $cityId)->value('name');
            $areaName = Db::table('evt_city')->where('citycode', $areaId)->value('name');
            $model->address_name = $provinceName . $cityName . $areaName;
            $model->address_details = $addressDetails;
            $model->realname = $realname;
            $model->mobile = $mobile;
            if ($remarks) {
                $model->remarks = $remarks;
            }

            if ($isDefault == 1) {
                UserAddress::where('user_id', $user['id'])->update([
                    'is_default' => 0
                ]);
            }
            $model->is_default = $isDefault;
            $model->save();
            $arr['code'] = 200;
            $arr['message'] = '成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }


}